package com.firexxx.study.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

/**
 * @description: TODO
 * @throws:
 * @author: zhuangzhang2
 * @date: 2020/05/05 21:25
 */
@Component
public class BeanUtil implements ApplicationContextAware {

    public static ConfigurableApplicationContext applicationContext;

    public static <T> T getBean(Class<T> c){
        return applicationContext.getBean(c);
    }

    public static <T> T registerBean(String name, T obj){
        applicationContext.getBeanFactory().registerSingleton(name, obj);

        return obj;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        BeanUtil.applicationContext = (ConfigurableApplicationContext)applicationContext;
    }
}